<template>
	<view class="leaveNav">
		<view style="margin-left:45rpx;font-weight: 800;font-size: 42rpx;">{{titleHead}}
			<view class="leaveLine"></view>
		</view>
		<button v-if="userInfo=='部门经理'&&(titleHead!='报修状况'&&titleHead!='报修信息')&&isDuty!='1'"></button>
		<button v-else-if="!btnFont" class="leaveBtn" @click="submit()">提交</button>
		<button v-else-if="titleHead=='报修状况'||titleHead == '最新报修'||titleHead=='正在处理'||titleHead=='记录列表'" @click="skip()"
			class="leaveBtn">{{btnFont}}</button>
	</view>
</template>
<script>
	import * as imageConversion from 'image-conversion'
	export default {
		name: 'btnCom',
		props: {
			titleHead: String,
			btnFont: String,
			repairDesc: String,
			deviceIds: Array,
			photo: String,
			repArr: Array,
			repairId: String,
			dormitoryNo: String,
			classDeptId: String,
			type: String,
			buildingNo: String,
			className: String,
			classId: String,
			bdormitoryId: String,
			dormitoryId: String,

		},
		data() {
			return {
				isDuty: 0,
				userInfo: "",
				deviceIdArr: [],
				SbuildingNo: String,
				SclassName: String,
				SclassId: Number,
				SbdormitoryId: Number,
				SdormitoryId: '',
				SdormitoryNo: Number,
				SclassDeptId: Number,
			}
		},
		mounted() {
			var that = this
			uni.getStorage({
				key: 'userInfo',
				success: (res) => {
					that.userInfo = res.data
				}
			})
			uni.getStorage({
				key: 'isDuty',
				success: (res) => {
					that.isDuty = res.data
				}
			})
			this.$forceUpdate()
		},
		methods: {
			submit() {
				if (this.titleHead == '考勤状况') {
					this.$emit('classAttenceConfirm')
				} else if (this.titleHead == '违纪登记') {
					this.$emit('principleFun')
				} else if (this.titleHead == '报修信息' || this.titleHead == '报修状况') {
					this.$emit('maintenanceFun')
				} else if (this.titleHead == "宿舍卫生") {
					this.$emit("dormitoryFun")
				}

			},
			// base64转换成二进制
			convertBase64UrlToBlob(base64) {
				let urlData = base64.dataURL
				let type = base64.type
				let bytes = null
				if (urlData.split(',').length > 1) { //是否带前缀
					bytes = window.atob(urlData.split(',')[1]) // 去掉url的头，并转换为byte
				} else {
					bytes = window.atob(urlData)
				}
				// 处理异常,将ascii码小于0的转换为大于0
				let ab = new ArrayBuffer(bytes.length)
				let ia = new Uint8Array(ab)
				for (let i = 0; i < bytes.length; i++) {
					ia[i] = bytes.charCodeAt(i)
				}
				return new Blob([ab], {
					type: type
				})
			},
			skip() {

				if (this.titleHead == '最新报修' || this.titleHead == '正在处理') {
					this.deviceIdArr = []
					if (this.repArr != null) {
						this.repArr.forEach((item, index) => {
							console.log(item.state, index)
							if (item.state == true) {
								this.deviceIdArr.push(item.deviceId)
							}
						})

					}

					var status = 1
					if (this.titleHead == '最新报修') {
						if (this.dormitoryNo) {
							this.$api.maintenance.updateRepairStatus(this.repairId, status, this.deviceIdArr, this
								.repairDesc).then(res => {
								uni.showToast({
									title: "提交成功"
								})
								setTimeout(function() {
									uni.navigateTo({
										url: "/pages/maintenance/newestMaintenanceStatus"
									})
								}, 500)

							})
						} else {
							this.$api.maintenance.updateClassRepair(this.repairId, status, this.deviceIdArr, this
								.repairDesc).then(res => {
								uni.showToast({
									title: "提交成功"
								})
								setTimeout(function() {
									uni.navigateTo({
										url: "/pages/maintenance/newestMaintenanceStatus"
									})
								}, 500)
							})
						}


					} else {
						//  Repair/updateRepairUpload 完成维修 宿舍 带图片
						// ClassRepair/updateClassRepairUpload 班级 带图片
						status = 2
						// console.log(this.dormitoryNo,this.photo,"dorid")
						// return;
						if (this.dormitoryNo) {
							if (this.file) {
								var form = new FormData();
								form.append("upload", this.file);
								form.append("id", this.repairId);
								form.append("bdormitoryId", this.$route.query.bdormitoryId)
								form.append("deviceIds", this.deviceIdArr);
								form.append("repairDesc", this.repairDesc);
								form.append("status", status)
								this.$http.post('Repair/updateRepairUpload', form).then(res => {
									setTimeout(function() {
										uni.hideLoading();
									}, 2000);
									uni.showToast({
										title: "提交成功"
									})
									setTimeout(function() {
										uni.navigateTo({
											url: "/pages/maintenance/maintenanceIng"
										})
									}, 500)
								})

								
							} else {
								this.$api.maintenance.updateRepairStatus(this.repairId, status, this.deviceIdArr, this
									.repairDesc).then(res => {
									uni.showToast({
										title: "提交成功"
									})
									setTimeout(function() {
										uni.navigateTo({
											url: "/pages/maintenance/maintenanceIng"
										})
									}, 500)
								})
							}

						} else {
							if (this.file) {
								var form = new FormData();
								form.append("upload", this.file);
								form.append("id", this.repairId);
								form.append("repairDesc", this.repairDesc);
								form.append('classId', this.classId)
								form.append("status", status)
								form.append("deviceIds", this.deviceIdArr)
								this.$http.post('ClassRepair/updateClassRepairUpload', form).then(res => {
									setTimeout(function() {
										uni.hideLoading();
									}, 2000);
									uni.showToast({
										title: "提交成功"
									})
									setTimeout(function() {
										uni.navigateTo({
											url: "/pages/maintenance/maintenanceIng"
										})
									}, 500)

								})




								
							} else {
								this.$api.maintenance.updateClassRepair(this.repairId, status, this.deviceIdArr, this
									.repairDesc).then(res => {
									uni.showToast({
										title: "提交成功"
									})
									setTimeout(function() {
										uni.navigateTo({
											url: "/pages/maintenance/maintenanceIng"
										})
									}, 500)

								})
							}

						}

						// uni.navigateTo({
						// 	url:"/pages/maintenance/newestMaintenanceStatus"
						// })
					}


				} else if (this.titleHead == '记录列表') {
					console.log(this.btnFont, "font")
					if (this.btnFont == '入校登记') {
						uni.navigateTo({
							url: '/pages/dutyCheckWorkAttendance/dutyMorinAttendance'
						})
					} else if (this.btnFont == '午休登记') {
						uni.navigateTo({
							url: '/pages/dutyCheckWorkAttendance/dutyMoonAttendanceDetail'
						})
					} else if (this.btnFont == '课间违纪') {
						uni.navigateTo({
							url: '/pages/dutyCheckWorkAttendance/dutyAfterClassDetail'
						})
					} else if (this.btnFont == '课堂违纪') {
						uni.navigateTo({
							url: '/pages/dutyCheckWorkAttendance/dutyClassDetail'
						})
					}
				} else {
					// alert(1)
					// console.log(this.$emit("maintenanceStatusFun"))
					// this.$emit('maintenanceStatusFun')
					var that = this
					this.SdormitoryId = uni.getStorageSync("dormitoryId")
					this.SbuildingNo = uni.getStorageSync("buildingNo")
					this.SclassDeptId = uni.getStorageSync("classDeptId")
					// uni.getStorage({
					// 	key: 'dormitoryId',
					// 	success: (res) => {
					// 		that.SdormitoryId = res.data
					// 	}
					// })
					// uni.getStorage({
					// 	key: 'buildingNo',
					// 	success: (res) => {
					// 		that.SbuildingNo = res.data
					// 	}
					// })
					// uni.getStorage({
					// 	key: 'classDeptId',
					// 	success: (res) => {
					// 		that.SclassDeptId = res.data
					// 	}
					// })
					if (this.SdormitoryId) {
						// uni.getStorage({
						// 	key: 'dormitoryNo',
						// 	success: (res) => {
						// 		that.SdormitoryNo = res.data
						// 	}
						// })
						this.SdormitoryNo = uni.getStorageSync("dormitoryNo")
						uni.navigateTo({
							url: `/pages/maintenance/maintenanceDetail?dormitoryId=${this.SdormitoryId}&dormitoryNo=${this.SdormitoryNo}&buildingNo=${this.SbuildingNo}&type=报修&classDeptId=${this.SclassDeptId}`,
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					} else {
						var that = this
						that.SclassId = uni.getStorageSync("classId")
						that.SclassName = uni.getStorageSync("className")
						// uni.getStorage({
						// 	key: 'classId',
						// 	success: (res) => {
						// 		that.SclassId = res.data
						// 	}
						// })
						// uni.getStorage({
						// 	key: 'className',
						// 	success: (res) => {
						// 		that.SclassName = res.data
						// 	}
						// })
						// &classDeptId=${this.$route.query.classDeptId}
						uni.navigateTo({
							url: `/pages/maintenance/maintenanceDetail?classId=${this.SclassId}&className=${this.SclassName}&buildingNo=${this.buildingNo}&type=报修`,
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					}
				}

			},
		}
	}
</script>

<style>
	uni-button {}

	.leaveNav {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: row;
		font-size: 38rpx;
		color: blue;
		width: 100%;
		padding: 50upx
	}

	.leaveBtn {
		background-image: url(https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/tubiao-01.png);
		background-size: 100%;
		width: 260rpx;
		height: 80rpx;
		color: white;
		font-size: 38rpx;
		line-height: 80rpx;
		font-weight: 0;
		margin-left: 100rpx;
		background-color: #FFFFFF !important;
		border: 0 !important
	}

	.leaveLine {
		width: 220upx;
		height: 8upx;
		background: rgba(239, 220, 11, 1.0);
		opacity: 0.78;
		position: relative;
		bottom: -10upx;
		left: -30upx;
	}

	.leaveMgm {
		color: #808080;
		font-size: 28upx;
	}

	.leaveCheck {
		margin-left: 60upx;
		margin-bottom: 30upx;

	}
</style>
